/* @(#)testshm.c
 * Time-stamp: <2008-10-25 19:32:33 root>
 * Copyright 2008 Julian Qian
 * Author: root@localhost.localdomain
 * Version: $Id: testshm.c,v 0.0 2008/10/08 07:36:54 root Exp $
 * Keywords: 
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <utmpx.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <math.h>
#include <getopt.h>
#include <syslog.h>

#define SHM_ESMS_SIZE 5000 /* ws_alarm_receive */
#define SHM_DEVICE "/dev/shm/ehome"

int main(int argc, char *argv[])
{
	char pstr[SHM_ESMS_SIZE] = "13811089645:cf";

	if(argc!=2){
	}else{
		memcpy(pstr, argv[1], SHM_ESMS_SIZE);
	}
	printf("shm content: %s\n", pstr);
	
	
	int shm_id;
	key_t shm_key;
	char *str_map;
	
	char shm_dev[] = SHM_DEVICE;
	if((shm_key = ftok(shm_dev, 't')) == -1){
		perror("ftok error!");
		return -1;
	}
	shm_id = shmget(shm_key, SHM_ESMS_SIZE, 0644 | IPC_CREAT);
	if(shm_id == -1){
		perror("shmget failed!");
		return -1;		
	}
	str_map = (char *)shmat(shm_id, NULL, 0);
	
	/* $info format: 1331331334:msg1,1321321323:msg2,.. */

	strncpy(str_map, pstr, SHM_ESMS_SIZE);

	/* printf("$$DEFENSE!\n SHARE memory: -->%s<--\n", str_map); */

	if(shmdt(str_map) == -1){
		perror("detach failed!");
		return -1;		
	}

	printf("Done!\n");
    return 0;
}


